//
//  SetIDViewController.m
//  Login
//
//  Created by mac on 17/3/20.
//  Copyright © 2017年 ZF. All rights reserved.
//

#import "SetIDViewController.h"
#import "NetworkLink.h"
@interface SetIDViewController () <UITextFieldDelegate>
{
    AppDelegate *delegate;
}
@property (weak, nonatomic) IBOutlet UITextField *name;

@end

@implementation SetIDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    delegate = [Helps app];
    self.name.text = delegate.userName;
    self.name.clearButtonMode =  UITextFieldViewModeAlways;
    self.name.returnKeyType = UIReturnKeyDone;
    self.name.delegate = self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.name resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self finish];
    return YES;
}

- (IBAction)onClick:(id)sender
{
    [self finish];
}

- (void)finish
{
    if([self.name.text isEqualToString:@""])
    {
        [Helps showTips:@"输入ID不能为空" forTime:1 yOffset:1];
        return ;
    }
    [NetworkLink setUserName:self.name.text Password:delegate.password Department:NULL Id:delegate.userID complete:^(BOOL ok) {
        if(ok)
        {
            delegate.userName = self.name.text;
            [delegate.userdefault setObject:delegate.userName forKey:@"UserName"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"fresh" object:nil];
            [Helps showTips:@"修改成功" forTime:1 inView:[Helps window] yOffset:1];
            [self.navigationController popViewControllerAnimated:YES];
            
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
